/**
 * @description 判断是否为空
 * @returns {boolean}
 * @param val
 */
export function isNull(val) {
	/**
	 * 判断是否为空
	 *  val
	 * @returns
	 */
	return (
		val === undefined ||
		val == null ||
		val === "" ||
		val === "" ||
		val === "undefined" ||
		val === "null" ||
		val === "NULL"
	);
}

/**
 * 比较两个日期大小 
 * @param {Object} date1
 * @param {Object} date2
 */
export function compareDates(date1, date2) {
	const d1 = new Date(date1);
	const d2 = new Date(date2);

	if (d1.getTime() < d2.getTime()) {
		return -1; // date1 小于 date2
	} else if (d1.getTime() > d2.getTime()) {
		return 1; // date1 大于 date2
	} else {
		return 0; // 两个日期相等
	}
}

/**
 * 获取未来7天的数据
 */
export function generateDateList() {
	const dates = [];
	const today = new Date();

	for (let i = 0; i <= 7; i++) {
		const date = new Date(today);
		date.setDate(today.getDate() + i); // 设置日期为今天加上i天
		const options = {
			month: 'long',
			day: 'numeric'
		}; // 设置日期格式选项，只包括月份和日期

		const formattedDate = date.toLocaleDateString(undefined, options); // 格式化日期
		const weekDay = date.toLocaleDateString(undefined, {
			weekday: 'long'
		}); // 仅获取星期几
		const year = date.getFullYear();
		const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需加1并补零
		const day = String(date.getDate()).padStart(2, '0'); // 补零

		const formattedDate2 = `${year}-${month}-${day}`; // 构建日期字符串
		dates.push({
			dateShow: formattedDate,
			date: formattedDate2,
			weekDay: weekDay
		});
	}

	return dates;
}

export function getChineseWeekday(dateString) {
	let date = new Date(dateString);
	let chineseWeekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
	let dayOfWeek = date.getDay();
	return chineseWeekdays[dayOfWeek];
}
export function formatTime(date) {
	const year = date.getFullYear();
	const month = (date.getMonth() + 1).toString().padStart(2, '0');
	const day = date.getDate().toString().padStart(2, '0');
	const hours = date.getHours().toString().padStart(2, '0');
	const minutes = date.getMinutes().toString().padStart(2, '0');
	const seconds = date.getSeconds().toString().padStart(2, '0');
	return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

/**
 * 日期格式化
 * @param {Object} date
 */
export function formatDate(date) {
	const year = date.getFullYear();
	const month = (date.getMonth() + 1).toString().padStart(2, '0');
	const day = date.getDate().toString().padStart(2, '0');
	return `${year}-${month}-${day}`;
}


// 日期加减

export function dateShifting(dateStr, i) {
	// 将字符串转换为Date对象
	let dateObj = new Date(dateStr);

	// 给日期加一天
	dateObj.setDate(dateObj.getDate() + i);

	// 将Date对象转换回YYYY-MM-DD格式的字符串
	let year = dateObj.getFullYear();
	let month = String(dateObj.getMonth() + 1).padStart(2, '0');
	let day = String(dateObj.getDate()).padStart(2, '0');

	return `${year}-${month}-${day}`;
}

export function formateDate(date) {
	const year = date.getFullYear();
	const month = (date.getMonth() + 1).toString().padStart(2, '0');
	const day = date.getDate().toString().padStart(2, '0');
	return `${year}-${month}-${day}`;
}

export function dateToAge(e) {
	if (e == null) {
		return "0岁"
	}

	var time1 = Date.parse(new Date())
	var time2 = Date.parse(new Date(e))
	var nDays = Math.abs(parseInt((time2 - time1) / 2626560 / 1000))
	return parseInt(nDays / 12) + '岁' + parseInt(nDays % 12) + '月'
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 判读是否为外链
 * @param path
 * @returns {boolean}
 */
export function isExternal(path) {
	return /^(https?:|mailto:|tel:)/.test(path);
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 密码至少包含大小写字母，数字，且不少于6位
 * @param str
 * @returns {boolean}
 */
export function isPassword(str) {
	const reg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{6,20}$/;
	return reg.test(str);
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 校验编号 编号只能为字母、数字或下划线
 * @param str
 * @returns {boolean}
 */
export function isCode(str) {
	const reg = /^\w+$/;
	return reg.test(str);
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 判断是否为数字
 * @param value
 * @returns {boolean}
 */
export function isNumber(value) {
	const reg = /^[0-9]*$/;
	return reg.test(value);
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 判断是否是名称
 * @param value
 * @returns {boolean}
 */
export function isName(value) {
	const reg = /^[\u4E00-\u9FA5a-zA-Z0-9_]+$/;
	return reg.test(value);
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 判断是否为IP
 * @param ip
 * @returns {boolean}
 */
export function isIP(ip) {
	const reg =
		/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
	return reg.test(ip);
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 判断是否是传统网站
 * @param url
 * @returns {boolean}
 */
export function isUrl(url) {
	const reg =
		/^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/;
	return reg.test(url);
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 判断是否是小写字母
 * @param str
 * @returns {boolean}
 */
export function isLowerCase(str) {
	const reg = /^[a-z]+$/;
	return reg.test(str);
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 判断是否是大写字母
 * @param str
 * @returns {boolean}
 */
export function isUpperCase(str) {
	const reg = /^[A-Z]+$/;
	return reg.test(str);
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 判断是否是大写字母开头
 * @param str
 * @returns {boolean}
 */
export function isAlphabets(str) {
	const reg = /^[A-Za-z]+$/;
	return reg.test(str);
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 判断是否是字符串
 * @param str
 * @returns {boolean}
 */
export function isString(str) {
	return typeof str === "string" || str instanceof String;
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 判断是否是数组
 * @param arg
 * @returns {arg is any[]|boolean}
 */
export function isArray(arg) {
	if (typeof Array.isArray === "undefined") {
		return Object.prototype.toString.call(arg) === "[object Array]";
	}
	return Array.isArray(arg);
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 判断是否是端口号
 * @param str
 * @returns {boolean}
 */
export function isPort(str) {
	const reg =
		/^([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/;
	return reg.test(str);
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 判断是否是手机号
 * @param str
 * @returns {boolean}
 */
export function isPhone(str) {
	const reg = /^1\d{10}$/;
	return reg.test(str);
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 判断是否是身份证号(第二代)
 * @param str
 * @returns {boolean}
 */
export function isIdCard(str) {
	const reg =
		/^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
	return reg.test(str);
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 判断是否是邮箱
 * @param str
 * @returns {boolean}
 */
export function isEmail(str) {
	const reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	return reg.test(str);
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 判断是否中文
 * @param str
 * @returns {boolean}
 */
export function isChina(str) {
	const reg = /^[\u4E00-\u9FA5]{2,4}$/;
	return reg.test(str);
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 判断是否为空
 * @param str
 * @returns {boolean}
 */
export function isBlank(str) {
	return (
		str == null ||
		false ||
		str === "" ||
		str.trim() === "" ||
		str.toLocaleLowerCase().trim() === "null"
	);
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 判断是否为固话
 * @param str
 * @returns {boolean}
 */
export function isTel(str) {
	const reg =
		/^(400|800)([0-9\\-]{7,10})|(([0-9]{4}|[0-9]{3})(-| )?)?([0-9]{7,8})((-| |转)*([0-9]{1,4}))?$/;
	return reg.test(str);
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 判断是否为数字且最多两位小数
 * @param str
 * @returns {boolean}
 */
export function isNum(str) {
	const reg = /^\d+(\.\d{1,2})?$/;
	return reg.test(str);
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 判断经度 -180.0～+180.0（整数部分为0～180，必须输入1到5位小数）
 * @param str
 * @returns {boolean}
 */
export function isLongitude(str) {
	const reg = /^[-|+]?(0?\d{1,2}\.\d{1,5}|1[0-7]?\d{1}\.\d{1,5}|180\.0{1,5})$/;
	return reg.test(str);
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 判断纬度 -90.0～+90.0（整数部分为0～90，必须输入1到5位小数）
 * @param str
 * @returns {boolean}
 */
export function isLatitude(str) {
	const reg = /^[-|+]?([0-8]?\d{1}\.\d{1,5}|90\.0{1,5})$/;
	return reg.test(str);
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description rtsp校验，只要有rtsp://
 * @param str
 * @returns {boolean}
 */
export function isRTSP(str) {
	const reg =
		/^rtsp:\/\/([a-z]{0,10}:.{0,10}@)?(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
	const reg1 =
		/^rtsp:\/\/([a-z]{0,10}:.{0,10}@)?(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5]):[0-9]{1,5}/;
	const reg2 =
		/^rtsp:\/\/([a-z]{0,10}:.{0,10}@)?(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\//;
	return reg.test(str) || reg1.test(str) || reg2.test(str);
}

// 判断的是否是JSON字符串
export function isObject(str) {
	if (typeof str == "object" && str) {
		console.log("对象");

		return true;
	} else {
		//不是就返回false
		return false;
	}
}

// 判断的是否是JSON字符串
export function isJson(str) {
	if (typeof str == "string") {
		try {
			var obj = JSON.parse(str);
			// 等于这个条件说明就是JSON字符串 会返回true
			if (typeof obj == "object" && obj) {
				return true;
			} else {
				//不是就返回false
				return false;
			}
		} catch (e) {
			return false;
		}
	}

	return false;
}